// jpushNotifi.js
//初始化极光推送
function initJpush(){
	var ajpush = api.require('ajpush');
	//初始化极光推送
	ajpush.init(function(ret) {
	    if (ret && ret.status){
	    }
	});
	setupJpushNotifi(ajpush);
	setTimeout(function(){
		setupJpush(ajpush);
	}, 1000);
}
//设置极光推送
function setupJpush(ajpush){
    var param = {alias: $api.getStorage('cfnetppuid')};
	ajpush.bindAliasAndTags(param,function(ret) {
	    var statusCode = ret.statusCode;
	    if (statusCode != 0) {
	    	ajpush.bindAliasAndTags(param,function(ret) {});
	    }
	});
	//设置消息状态
	var msgStatus = $api.getStorage('msgStatus');
	if (msgStatus == 'false') {
        ajpush.stopPush(function(ret) {});
	}else{
		// loadNotifiStatus();//设置本地消息
		ajpush.resumePush(function(ret) {});
	}
}
//保存消息到本地
function saveMsgToDB(dataParam){
	insertData('t_message', dataParam, function(ret, err){
		// if (ret.status) {
		// 	setupMsgWithDBData(dataParam);
		// }
	});
}
function setupJpushNotifi(ajpush){
	ajpush.setListener(function(ret){
		// var id = ret.id;
		// var title = ret.title;
		// var content = ret.content;
		var extra = ret.extra;
		if (extra.constructor == String) {
			extra = JSON.parse(extra);
		}
		var data = msgDataWithExtra(extra);
        saveMsgToDB(data);
    });
    var systemType = api.systemType;
	if (systemType == 'ios') {
		setupIOSNotifi();
	}else{
		setupAndroidNotifi();
	}
}
//设置Androad应用后台下接收通知
function setupAndroidNotifi(){
	api.addEventListener({name:'appintent'}, function(ret,err) {
	    if(ret && ret.appParam.ajpush){
	        var ajpushData = ret.appParam.ajpush;
	        // var id = ajpushData.id;
	        // var title = ajpushData.title;
	        // var content = ajpushData.content;
	        var extra = JSON.parse(ajpushData.extra);
	        var data = msgDataWithExtra(extra);
	        data['readstatus'] = 1;//点击消息则设置阅读状态未1
	        saveMsgToDB(data);
	        pushToMsgWindow(data);
	    }
	});
}
//设置iOS应用后台下接收通知
function setupIOSNotifi(){
	api.addEventListener({name:'noticeclicked'}, function(ret,err) {
	    if(ret && ret.value){
	        var ajpushData = ret.value;
	        // var content = ajpushData.content;
	        var extra = JSON.parse(ajpushData.extra);;
	        var data = msgDataWithExtra(extra);
	        data['readstatus'] = 1;//点击消息则设置阅读状态未1
	        saveMsgToDB(data);
	        pushToMsgWindow(data);
	    }
	});
}
function msgDataWithExtra(extra){
	if (extra.basetype == 0) {//如果是公告则刷新公告列表
		api.sendEvent({
		    name: 'refreshNoticeNotifi'
		});
	}
	return {
    	msgid : extra.msgid,
    	basetype : extra.basetype,
    	subtype : extra.subtype,
    	richtext : extra.richtext,
    	title : extra.title,
    	content : extra.content,
    	receiptime : getNowDate(),
    	readstatus : 0,
    	uid : $api.getStorage('cfnetppuid'),
    	jid : $api.getStorage('cfnetppjid'),
    	uniquekey : extra.msgid + $api.getStorage('cfnetppuid')
    };
}
function pushToMsgWindow(msgData){
	if (msgData.basetype == 0) {//公告消息
		api.openWin({useWKWebView:true,
		    name: 'TrainNotice',
		    url: './html/TrainNotice.html',
		    pageParam: msgData
		});
	}else if(msgData.basetype == 1){//系统消息
		api.openWin({useWKWebView:true,
		    name: 'Complement',
		    url: './html/Complement.html',
		    pageParam: msgData
		});
	}else{
		api.toast({
		    msg: '未知的消息类型'
		});
	}
}
//上传设备id
function uploadRid(){
	//    ajpush.getRegistrationId(function(ret) {
	//     var registrationId = ret.id;
	//     var params = {
	//     	uid : $api.getStorage('cfnetppuid'),
	//     	jid : $api.getStorage('cfnetppjid'),
	//     	registrationid : registrationId
	//     };
	//     //上传设备id
	//     cfnetppPOST(urlUploadReid,
	// 	params,
	// 	false,
	// 	function(data, status){
	// 		if (status == 'success') {
	// 			if (data.status == 1) {
	// 				// api.toast({
	// 				//     msg: '上传设备id成功'
	// 				// });
	// 			}else{
	// 				// api.toast({
	// 				//     msg: '上传设备id失败'
	// 				// });
	// 			}
	// 		}else{
	// 			// api.toast({
	// 			//     msg: '上传设备id失败'
	// 			// });
	// 		}
	// 	});
	// });
}